package com.zoe.vhr.controller.emp;

import com.zoe.vhr.model.*;
import com.zoe.vhr.service.*;
import com.zoe.vhr.util.R;
import com.zoe.vhr.util.RPage;
import com.zoe.vhr.utils.MultipartFileUtil;
import com.zoe.vhr.utils.POIUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author Zoe.Zhang
 * @Date: 2021/08/08
 * @Description 员工基本资料接口
 */
@RestController
@RequestMapping("/emp/basic")
@Validated
public class EmpBasicController {
    @Autowired
    EmployeeService employeeService;

    @Autowired
    NationService nationService;

    @Autowired
    PoliticsStatusService politicsStatusService;

    @Autowired
    JobLevelService jobLevelService;

    @Autowired
    PositionService positionService;

    @Autowired
    DepartmentService departmentService;

    @GetMapping("/listAllEmps")
    public R listAllEmp(Employee employee) {
        List<Employee> employees = employeeService.listAllEmp(employee);
        return R.success(employees);
    }

    @GetMapping("/listEmpPages")
    public R listEmpPages(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                          Employee employee) {
        List<Employee> employees = employeeService.listEmpPages(pageNum, pageSize, employee);
        return R.success(RPage.restPage(employees));
    }

    @GetMapping("/getMaxWorkId")
    public R getMaxWorkId() {
        String maxWorkId = employeeService.getMaxWorkId();
        return R.success(maxWorkId);
    }

    @PostMapping("/saveEmp")
    public R saveEmp(@Valid @RequestBody Employee employee) {
        int count = employeeService.saveEmp(employee);
        if (count > 0) {
            return R.success("添加成功");
        }
        return R.failed("添加失败");
    }

    @PutMapping("/updateEmp")
    public R updateEmp(@Valid @RequestBody Employee employee) {
        System.out.println(employee);
        int count = employeeService.updateEmp(employee);
        if (count > 0) {
            return R.success("修改成功");
        }
        return R.failed("修改失败");

    }

    @DeleteMapping("/deleteEmp/{empId}")
    public R deleteEmp(@PathVariable("empId") Integer empId) {
        int count = employeeService.deleteEmp(empId);
        if (count > 0) {
            return R.success("删除成功");
        }
        return R.failed("删除失败");
    }

    @GetMapping("/listAllNations")
    public R listAllNation() {
        List<Nation> nations = nationService.listAllNation();
        return R.success(nations);
    }

    @GetMapping("/listAllPoliticsStatus")
    public R listAllPoliticsStatus() {
        List<Politicsstatus> politicsstatuses = politicsStatusService.listAllPoliticsStatus();
        return R.success(politicsstatuses);
    }

    @GetMapping("/listAllJobLevels")
    public R listAllJobLevel() {
        List<JObLevel> jObLevels = jobLevelService.listJobLevels();
        return R.success(jObLevels);
    }

    @GetMapping("/listAllPositions")
    public R listAllPosition() {
        List<Position> positionServices = positionService.listPositions();
        return R.success(positionServices);
    }

    @GetMapping("/listAllDepartments")
    public R listAllDepartments() {
        List<Department> departments = departmentService.listAllDepartmentName();
        return R.success(departments);
    }

    @GetMapping("/exportEmp")
    public ResponseEntity<byte[]> exportEmp(Employee employee) {
        List<Employee> employees = employeeService.listAllEmp(employee);
        return POIUtil.exportEmpExcel(employees);
    }

    @PostMapping("/importExcel")
    public R importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartFile multipartFile = MultipartFileUtil.getSingleFile(request);

        List<Employee> employeeList = null;
        try {
            employeeList = POIUtil.excelToEmployee(multipartFile,
                    nationService.listAllNation(),
                    politicsStatusService.listAllPoliticsStatus(),
                    departmentService.listAllDepartmentName(),
                    positionService.listPositions(),
                    jobLevelService.listJobLevels());
        }
        catch (Exception e) {
            throw new RuntimeException("导入数据异常");
        }
        int count = employeeService.saveEmpByExcel(employeeList);
        return R.success("上传成功");
    }

    @DeleteMapping("/deleteBatchEmpByIds/{empIds}")
    public R deleteBatchEmpByIds(@PathVariable("empIds") Integer[] empIds) {
        int count = employeeService.deleteBatchEmpByIds(empIds);
        if (count > 0) {
            return R.success("删除成功");
        }
        return R.failed("删除失败");
    }
}
